efform_onload = function ()
{	
	hideAndShowSubButtons(false);
	efgrid.submitInqu( "ef_grid_r", "ZM","ZMCG2001","query");
	var info = new EiInfo();
	$('#ef_region_detail').hide();
	EiCommunicator.send("ZMCG2002", "query", info, ajax_callback);	
//	departmentChanged();
}; 

var ajax_callback = {
	onSuccess : function(eiInfo) {
		efwindow.hide(); 
		var grid = efgrid.getGridObject("ef_grid_d");
		grid.refresh(eiInfo);
	},
	onFail : function(eMsg) {}
};
button_query_onclick = function () 
{   	
	efgrid.submitInqu( "ef_grid_r", "ZM","ZMCG2001","query");
}
button_update_onclick = function (){ 
	if (isSelected("ef_grid_r") == 0)return; 
	efgrid.submitForm( "ef_grid_r", "ZM","ZMCG2001","update",true);
}
button_insert_onclick = function (){
	if (isSelected("ef_grid_r") == 0)return; 
	efgrid.submitForm( "ef_grid_r", "ZM","ZMCG2001","insert",true);
} 
button_send_onclick = function (){
	var grid=isSelected("ef_grid_r");
	if (grid == 0)return; 
	var rows=grid.getCheckedRows();
	for(var i=0;i<rows.length;i++)
	{
		if(grid.getCellValueByColumnName(rows[i],"fdStatus")!="草拟")
		{
			EFAlert("只能发送草拟的联系单！"); return;
		}
	}
	EFConfirm('你确认要发送吗?', '确认框', function(r) {
		if(r)efgrid.submitForm( "ef_grid_r", "ZM","ZMCG2001","sendContact",true);
	});
//	if (isSelected("ef_grid_r") == 0)return; 
//	efgrid.submitForm( "ef_grid_r", "ZM","ZMCG2001","sendContact",true);
} 
button_delete_onclick = function (){
	var grid=isSelected("ef_grid_r");
	if (grid == 0)return; 
	var rows=grid.getCheckedRows();
	for(var i=0;i<rows.length;i++)
	{
		if(grid.getCellValueByColumnName(rows[i],"fdStatus")!="草拟")
		{
			EFAlert("只能对草拟的记录进行删除！"); return;
		}
	}
	EFConfirm('你确认要删除吗?', '确认框', function(r) {
		if(r)efgrid.submitForm( "ef_grid_r", "ZM","ZMCG2001","delete",true);
	});
	hideAndShowSubButtons(false);
}


efgrid_onRowClicked = function( grid_id, row_index ){
	if(grid_id == "ef_grid_r"){ 
		$('#ef_region_detail').show();
		var grid = efgrid.getGridObject(grid_id);
		var fdKeyId=grid.getCellValueByColumnName(row_index,"fdKeyId");
		var fdTitle=grid.getCellValueByColumnName(row_index,"fdTitle");
		var fdContent=grid.getCellValueByColumnName(row_index,"fdContent");
		var fdSendPerson=grid.getCellValueByColumnName(row_index,"fdSendPerson");
		var fdSendTime=grid.getCellValueByColumnName(row_index,"fdSendTime");		
		var fdStatus=grid.getCellValueByColumnName(row_index,"fdStatus");
		//隐藏/显示从表按钮
		if(fdStatus!='草拟') hideAndShowSubButtons(false);
		else hideAndShowSubButtons(true); 
		$('#fdContactlistId').val(fdKeyId);
		$('#fdTitle').val(fdTitle);
		$('#fdContent').val(fdContent);
		$('#fdSendPerson').val(fdSendPerson);
		$('#fdSendTime').val(fdSendTime);
		var info = new EiInfo();
		info.set("fdContactlistId",fdKeyId);  
		EiCommunicator.send("ZMCG2002", "query", info, ajax_callback);  
	}
}; 

/********************************************************************************
 ***********************************从表操作**************************************** 
 ********************************************************************************/
button_insert1_onclick = function (){ 
	if (isSelected("ef_grid_d") == 0)return; 
//	efgrid.submitForm( "ef_grid_d", "ZM","ZMCG2002","insert",true);
	var info = getSCZXEiInfos("ZMCG2001","ef_grid_d");//ZMCN10 为EFFORM id，ef_grid_r为grid id（paintId）
	if (null != info) { 
		EiCommunicator.send("ZMCG2002", "insert", info, validate_callback);
	}
}
button_delete1_onclick = function (){	
	var grid=isSelected("ef_grid_d");
	if (grid == 0)return; 
	
	var rows=grid.getCheckedRows();
	for(var i=0;i<rows.length;i++)
	{
		if(grid.getCellValueByColumnName(rows[i],"fdStatus")!="未反馈")
		{
			EFAlert("只能对未反馈的记录进行删除！"); return;
		}

	}
	EFConfirm('你确认要删除吗?', '确认框', function(r) {
		if(r){
			efgrid.submitForm( "ef_grid_d", "ZM","ZMCG2002","delete",true);
		}
	}); 
}  
var ajax_callbackconfirm = {
	onSuccess : function(eiInfo) {
		efwindow.hide(); 
		var grid = efgrid.getGridObject("ef_grid_d");
		grid.refresh(eiInfo);
	}, 
	onFail:function(eMsg)
	{
		alert("确认失败，原因："+eMsg);
	}
}
var validate_callback={
		onSuccess:function(eiInfo){
			if(eiInfo.getStatus()==-1){
				EFAlert("操作失败，原因："+eiInfo.getMsg());
			}else{
				 efwindow.hide();
				 var grid = efgrid.getGridObject("ef_grid_d");
				 grid.refresh(eiInfo);
			}
		},
		onFail:function(eMsg)
		{
			EFAlert("操作失败，原因："+eMsg); 
		}
}
//function departmentChanged()
//{ 
//	  var departmentid=$('#inqu_status1-0-fdDepartmentId').val(); 
//	  var info=_getEi();
//	  var eiBlock=info.getBlock("cmes.cp.department");
//	  for(var i=0;i<eiBlock.getRows().length;i++)
//	  {
//		  var _display = eiBlock.getCell(i,"orgName");
//		  var _value = eiBlock.getCell(i,"orgCode");
//		  if(_value==departmentid)
//		  { 
//			  $('#inqu_status1-0-fdDepartment').val(_display);
//		  }
//	  }
//}
var hideAndShowSubButtons=function(status)
{
	if(!status)
	{
		efbutton.setButtonStatus("insert1", false);
		efbutton.setButtonStatus("delete1", false);
	}else
	{
		efbutton.setButtonStatus("insert1", true);
		efbutton.setButtonStatus("delete1", true);
	}
}
efgrid_afterAddNewRow = function(grid_id){ 
	var grid = efgrid.getGridObject(grid_id);
	var rows = grid.getCheckedRows(); 
	if(grid_id=="ef_grid_d")
	{
		var fdSendPerson=$('#fdSendPerson').val();
		var fdSendTime=$('#fdSendTime').val();
		var fdContactlistId=$('#fdContactlistId').val();
		var fdTitle=$('#fdTitle').val();
		var fdContent=$('#fdContent').val();
		for(var i = 0 ; i < rows.length;i++){
//			grid.setCellValue(rows[i],3,fdSendPerson,TYPE_DATA);
//			grid.setCellValue(rows[i],4,fdSendTime,TYPE_DATA);
//			grid.setCellValue(rows[i],0,fdContactlistId,TYPE_DATA);
			grid.setCellValue(rows[i],1,fdTitle,TYPE_DATA);
//			grid.setCellValue(rows[i],5,fdContent,TYPE_DATA);
		}
	}
}
